<%@ tag language="java" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/views/commons/taglib.jsp"%>

<%@ attribute name="id" type="java.lang.String" required="true" description="编号"%>
<%@ attribute name="url" type="java.lang.String" required="true" description="树结构数据地址"%>
<%@ attribute name="jsonRoot" type="java.lang.String" required="false" description="数据路径"%>
<%@ attribute name="parentIdKey" type="java.lang.String" required="false" description="数据路径"%>
<%@ attribute name="checked" type="java.lang.Boolean" required="false" description="是否显示复选框，如果不需要返回父节点，请设置notAllowSelectParent为true"%>
<%@ attribute name="extId" type="java.lang.String" required="false" description="排除掉的编号（不能选择的编号）"%>
<%@ attribute name="isAll" type="java.lang.Boolean" required="false" description="是否列出全部数据，设置true则不进行数据权限过滤（目前仅对Office有效）"%>
<%@ attribute name="module" type="java.lang.String" required="false" description="过滤栏目模型（只显示指定模型，仅针对CMS的Category树）"%>
<div>
<!-- <div style="position:absolute;right:8px;top:5px;cursor:pointer;" onclick="search();">
		<i class="icon-search"></i><label id="txt">搜索</label>
	</div> -->
	<!-- <div id="search" class="form-search " style="padding:10px 0 0 13px;">
		<label for="key" class="control-label" style="padding:5px 5px 3px 0;">关键字：</label>
		<input type="text" class="empty" id="key" name="key" maxlength="50" style="width:110px;">
		<button class="btn" id="btn" onclick="searchNode();return false;">搜索</button>
	</div> -->
	<div id="${id}Tree" class="ztree" style="padding:15px 20px;"></div>
</div>

<link rel="stylesheet" href="${ctxStatic}/ztree/css/demo.css" type="text/css">
<link rel="stylesheet" href="${ctxStatic}/ztree/css/metroStyle/metroStyle.css" type="text/css">
<script src="${ctxStatic}/ztree/js/jquery.ztree.all.min.js"  type="text/javascript"></script>
<script type="text/javascript">
$(function(){
	var key, lastValue = "", nodeList = [], /* type = getQueryString("type", "${url}"); */
	type = 1;
	
	var tree, setting = {view:{selectedMulti:false,dblClickExpand:false},check:{enable:"${checked}",nocheckInherit:true},
			async:{enable:(type==3),url:"",autoParam:["id=${parentIdKey}"]},
			data:{simpleData:{enable:true,pIdKey: '${empty parentIdKey?"parent_id":parentIdKey}',}},callback:{<%--
				beforeClick: function(treeId, treeNode){
					if("${checked}" == "true"){
						//tree.checkNode(treeNode, !node.checked, true, true);
						tree.expandNode(treeNode, true, false, false);
					}
				}, --%>
				onClick:function(event, treeId, treeNode,clickFlag){
					tree.expandNode(treeNode);
					if(typeof ${id}TreeSelectCallBack == 'function'){
						${id}TreeSelectCallBack(event, treeId, treeNode, clickFlag);
					}
				},onCheck: function(e, treeId, treeNode){
					var nodes = tree.getCheckedNodes(true);
					for (var i=0, l=nodes.length; i<l; i++) {
						tree.expandNode(nodes[i], true, false, false);
					}
					return false;
				},onAsyncSuccess: function(event, treeId, treeNode, msg){
					var nodes = tree.getNodesByParam("${parentIdKey}", treeNode.id, null);
					for (var i=0, l=nodes.length; i<l; i++) {
						try{tree.checkNode(nodes[i], treeNode.checked, true);}catch(e){}
						//tree.selectNode(nodes[i], false);
					}
					selectCheckNode();
				},onDblClick: function(){//<c:if test="${!checked}">
					top.$.jBox.getBox().find("button[value='ok']").trigger("click");
					//$("input[type='text']", top.mainFrame.document).focus();//</c:if>
				}
			}
		};
	
	function expandNodes(nodes) {
		if (!nodes) return;
		for (var i=0, l=nodes.length; i<l; i++) {
			tree.expandNode(nodes[i], true, false, false);
			if (nodes[i].isParent && nodes[i].zAsync) {
				expandNodes(nodes[i].children);
			}
		}
	}
	
		$.get("${url}?extId=${extId}&isAll=${isAll}&module=${module}&t="+ new Date().getTime(), function(zNodes){
			// 初始化树结构
			tree = $.fn.zTree.init($("#${id}Tree"), setting, zNodes${empty jsonRoot?"":"."}${jsonRoot});
			window.top.${id}Tree = tree;
			// 默认展开一级节点
			var nodes = tree.getNodesByParam("level", 0);
			for(var i=0; i<nodes.length; i++) {
				tree.expandNode(nodes[i], true, false, false);
			}
			//异步加载子节点（加载用户）
			var nodesOne = tree.getNodesByParam("isParent", true);
			for(var j=0; j<nodesOne.length; j++) {
				tree.reAsyncChildNodes(nodesOne[j],"!refresh",true);
			}
			selectCheckNode();
		});
		key = $("#key");
		key.bind("focus", focusKey).bind("blur", blurKey).bind("change cut input propertychange", searchNode);
		key.bind('keydown', function (e){if(e.which == 13){searchNode();}});
		setTimeout("search();", "300");
	
	
	// 默认选择节点
	function selectCheckNode(){
		var ids = "${selectIds}".split(",");
		for(var i=0; i<ids.length; i++) {
			var node = tree.getNodeByParam("id", (type==3?"u_":"")+ids[i]);
			if("${checked}" == "true"){
				try{tree.checkNode(node, true, true);}catch(e){}
				tree.selectNode(node, false);
			}else{
				tree.selectNode(node, true);
			}
		}
	}
  	function focusKey(e) {
		if (key.hasClass("empty")) {
			key.removeClass("empty");
		}
	}
	function blurKey(e) {
		if (key.get(0).value === "") {
			key.addClass("empty");
		}
		searchNode(e);
	}
	
	//搜索节点
	function searchNode(e) {
		// 取得输入的关键字的值
		var value = $.trim(key.get(0).value);
		
		// 按名字查询
		var keyType = "name";<%--
		if (key.hasClass("empty")) {
			value = "";
		}--%>
		
		// 如果和上次一次，就退出不查了。
		if (lastValue === value) {
			return;
		}
		
		// 保存最后一次
		lastValue = value;
		
		var nodes = tree.getNodes();
		// 如果要查空字串，就退出不查了。
		if (value == "") {
			showAllNode(nodes);
			return;
		}
		hideAllNode(nodes);
		nodeList = tree.getNodesByParamFuzzy(keyType, value);
		updateNodes(nodeList);
	}
	
	//隐藏所有节点
	function hideAllNode(nodes){			
		nodes = tree.transformToArray(nodes);
		for(var i=nodes.length-1; i>=0; i--) {
			tree.hideNode(nodes[i]);
		}
	}
	
	//显示所有节点
	function showAllNode(nodes){			
		nodes = tree.transformToArray(nodes);
		for(var i=nodes.length-1; i>=0; i--) {
			/* if(!nodes[i].isParent){
				tree.showNode(nodes[i]);
			}else{ */
				if(nodes[i].getParentNode()!=null){
					tree.expandNode(nodes[i],false,false,false,false);
				}else{
					tree.expandNode(nodes[i],true,true,false,false);
				}
				tree.showNode(nodes[i]);
				showAllNode(nodes[i].children);
			/* } */
		}
	}
	
	//更新节点状态
	function updateNodes(nodeList) {
		tree.showNodes(nodeList);
		for(var i=0, l=nodeList.length; i<l; i++) {
			
			//展开当前节点的父节点
			tree.showNode(nodeList[i].getParentNode()); 
			//tree.expandNode(nodeList[i].getParentNode(), true, false, false);
			//显示展开符合条件节点的父节点
			while(nodeList[i].getParentNode()!=null){
				tree.expandNode(nodeList[i].getParentNode(), true, false, false);
				nodeList[i] = nodeList[i].getParentNode();
				tree.showNode(nodeList[i].getParentNode());
			}
			//显示根节点
			tree.showNode(nodeList[i].getParentNode());
			//展开根节点
			tree.expandNode(nodeList[i].getParentNode(), true, false, false);
		}
	}
	
	// 开始搜索
	function search() {
		$("#search").slideToggle(200);
		$("#txt").toggle();
		$("#key").focus();
	}
})

		
	</script>
	